<?php

namespace web\api\controller;

/**
 * Description of News
 * 文章
 */
class News extends UserBase {

    /**
     * 文章列表
     */
    public function newsSpecialList(){
        try {
            $m = new \addons\news\model\News();
            $newsPraise = new \addons\news\model\NewsPraise();
            $filter = 'is_show = 1';
            $fields = ' * ';
            $order = ' id desc ';
            $list = $m->getNewsList($this->getPageIndex(), $this->getPageSize(), $filter, $fields, $order);

            foreach ($list as &$v) {
                $v['content'] = htmlspecialchars_decode($v['content']);
                $news_find = $newsPraise->where(['user_id' => $this->user_id, 'news_id' => $v['id']])->find();
                $v['is_praise'] = 0;
                if($news_find){
                    $v['is_praise'] = 1;
                }
                $v['content'] = htmlspecialchars_decode($v['content']);
            }
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }
	
	public function test1()
	{
		$c1 = $this->getPostData();print_r($c1);
		
		
	}
    /**
     *  专题详情
     */
    public function newsDetails(){
        $m = new \addons\news\model\News();
		//$id = $this->getParamVal('id',true);
		
        try {
            $id = $this->getParamVal('id');//文章ID
            $data = $m->getDetail($id);
            $data['content'] = htmlspecialchars_decode($data['content']);

            $newsPraise = new \addons\news\model\NewsPraise();
            $news_find = $newsPraise->where(['user_id' => $this->user_id, 'news_id' => $data['id']])->find();
            $data['is_praise'] = 0;
            if($news_find){
                $data['is_praise'] = 1;
            }
            return $this->successJSON($data);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 点赞操作
     */
    public function updatePraise(){
        try {
            $newsM = new \addons\news\model\News();
            $newsPraiseM = new \addons\news\model\NewsPraise();
            $is_praise = $this->getParamVal('is_praise');
            $news_id = $this->getParamVal('news_id');
            if($is_praise == 1){
                $praise_find = $newsPraiseM->where(['news_id' => $news_id, 'user_id' => $this->user_id])->find();
                if($praise_find) return $this->failJSON('请勿重复点击');
                $re = $newsPraiseM->add(['news_id' => $news_id, 'user_id' => $this->user_id, 'update_time' => NOW_DATETIME]);
                if(!$re) return $this->failJSON('点赞失败');
                $newsM->where(['id' => $news_id])->setInc('praise_number', 1);
            }else{
                $praise_find = $newsPraiseM->where(['news_id' => $news_id, 'user_id' => $this->user_id])->find();
                if(empty($praise_find)) return $this->failJSON('请勿重复点击');
                $re = $newsPraiseM->where(['news_id' => $news_id, 'user_id' => $this->user_id])->delete();
                if(!$re) return $this->failJSON('取消赞失败');
                $newsM->where(['id' => $news_id])->setDec('praise_number', 1);
            }
            return $this->successJSON();
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

}
